## Evaluate Methodolgy Class Labs
## Ryan T. Moore
## 11 July 2014

evaluateLabs <- function(x = data, n.tr = 3, id.name = "id", bl.vars = "math", w = w, n.rands = 100){
  
  n <- nrow(x)
  b <- block(x, block.vars = bl.vars, n.tr = n.tr, id.vars = id.name, weight = w)
  num.groups <- ncol(b$blocks[[1]]) - 1
  
  store.gp.stats <- data.frame(method = NA, rand = NA, variable = NA, grp = NA, mean = NA, sd = NA)
  store.rand.summs <- data.frame(method = NA, rand = NA, variable = NA, dm.stat = NA, dm.p = NA, dsd.stat = NA, dsd.p = NA)
    
  min.vars <- c("id", "bla", "cr", bl.vars)
  
  for(i in 1:n.rands){
    
    a <- assignment(b)
    
    x$cr <- sample(rep(1:num.groups, ceiling(n/num.groups)))[1:n]
    x$bl <- x$bla <- NA
    
    for(row.idx in 1:nrow(x)){
      x$bl[row.idx] <- ceiling(which(b$blocks[[1]] == x$id[row.idx])/nrow(b$blocks[[1]]))
      x$bla[row.idx] <- ceiling(which(a$assg[[1]] == x$id[row.idx])/nrow(a$assg[[1]]))
    }
    
    x.min <- x[, min.vars]
    molten <- melt(x.min, id.vars = c("id", "bla", "cr"))
    
    ## Summary stats by group:
    cr.tmp <- ddply(molten, .(cr, variable), summarise, mean = mean(value), sd = sd(value))
    bla.tmp <- ddply(molten, .(bla, variable), summarise, mean = mean(value), sd = sd(value))
    cr.tmp$method <- "cr"
    bla.tmp$method <- "bl" ## name change
    cr.tmp$grp <- cr.tmp$cr
    bla.tmp$grp <- bla.tmp$bla
    
    cr.tmp$rand <- bla.tmp$rand <- i
    cr.tmp <- cr.tmp[, c("method", "rand", "variable", "grp", "mean", "sd")]
    bla.tmp <- bla.tmp[, c("method", "rand", "variable", "grp", "mean", "sd")]
    
    store.gp.stats <- rbind(store.gp.stats, bla.tmp, cr.tmp)
    
    ## Tests of different locations:
    cr.tmp <- ddply(molten, .(variable), summarise, dm.stat = kruskal.test(value ~ cr)$statistic, dm.p = kruskal.test(value ~ cr)$p.value, dsd.stat = NA, dsd.p = NA)
    bla.tmp <- ddply(molten, .(variable), summarise, dm.stat = kruskal.test(value ~ bla)$statistic, dm.p = kruskal.test(value ~ bla)$p.value, dsd.stat = NA, dsd.p = NA)
    cr.tmp$method <- "cr"
    bla.tmp$method <- "bl"
    
    cr.tmp$rand <- bla.tmp$rand <- i
    cr.tmp <- cr.tmp[, c("method", "rand", "variable", "dm.stat", "dm.p", "dsd.stat", "dsd.p")]
    bla.tmp <- bla.tmp[, c("method", "rand", "variable", "dm.stat", "dm.p", "dsd.stat", "dsd.p")]
    
    store.rand.summs <- rbind(store.rand.summs, bla.tmp, cr.tmp)
    
  }
  
  store.gp.stats <- store.gp.stats[apply(is.na(store.gp.stats), 1, mean) != 1, ]
  store.rand.summs <- store.rand.summs[apply(is.na(store.rand.summs), 1, mean) != 1, ]
  
  row.names(store.gp.stats) <- (1:nrow(store.gp.stats))
  row.names(store.rand.summs) <- (1:nrow(store.rand.summs))
  
  output <- list(store.gp.stats = store.gp.stats, store.rand.summs = store.rand.summs)
  
  return(output)
}
